package com.yqg.manage.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.manage.common.interceptor.SysLog;
import com.yqg.manage.service.IPlaceService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqg.manage.common.Result;

import com.yqg.manage.service.IPlaceTypeService;
import com.yqg.manage.entity.PlaceType;

import org.springframework.web.bind.annotation.RestController;

/**
 * PlaceTypeController 前端控制器
 * @author zxf
 * @since 2024-07-03
 */
@RestController
@RequestMapping("/place-type")
public class PlaceTypeController {

    @Resource
    private IPlaceTypeService placeTypeService;

    @Resource
    private IPlaceService placeService;

    /**
    *  新增或者更新
    */
    @PostMapping("/edit")
    @SysLog("新增或者更新企事业单位类型")
    public Result save(@RequestBody PlaceType placeType) {
        LambdaQueryWrapper<PlaceType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PlaceType::getName, placeType.getName())
                .ne(placeType.getId() != null, PlaceType::getId, placeType.getId());
        PlaceType one = placeTypeService.getOne(queryWrapper);
        if (one != null) {
            return Result.error("该商户类型已存在");
        }
        return Result.success(placeTypeService.saveOrUpdate(placeType));
    }
    /**
    *  删除
    */
    @GetMapping("delete/{id}")
    @SysLog("删除企事业单位类型")
    public Result delete(@PathVariable Integer id) {
        PlaceType placeType =placeTypeService.getById(id);
        return Result.success(placeTypeService.removeById(placeType));
    }
    /**
    *  查询所有
    */
    @GetMapping("/allType")
    public Result findAll() {
        return Result.success(placeTypeService.list());
    }
    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(placeTypeService.getById(id));
    }
    /**
    *  分页查询
    */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                           String name) {
        QueryWrapper<PlaceType> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", name);
        queryWrapper.orderByDesc("id");
        return Result.success(placeTypeService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

}

